package com.ruoyi.web.controller.doulai;

import java.text.DateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.system.domain.OperLog;
import com.ruoyi.system.domain.OperLogRequestBean;
import com.ruoyi.system.domain.SysLogininfor;
import com.ruoyi.system.service.IOperLogService;
import com.ruoyi.system.service.ISysLogininforService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 用户 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-01-06
 */
@Controller
@RequestMapping("/doulai/operLog")
public class OperLogController extends BaseController
{
    private String prefix = "doulai/operLog";
	
	@Autowired
	private IOperLogService operLogService;

	@Autowired
	private ISysLogininforService logininforService;

	@GetMapping()
	public String operLog()
	{
	    return "/pages/operating-record/record";
	}

	@GetMapping("/freeze")
	public String freezeLog()
	{
		return "/pages/operating-record/freeze-mation";
	}

	@GetMapping("/relieve")
	public String relieve(Long id)
	{
		return "/pages/operating-record/relieve-mation";
	}

	@GetMapping("/present")
	public String present()
	{
		return "/pages/operating-record/present-mation";
	}
	
	/**
	 * 代理及账号冻结操作记录
	 */
	@RequiresPermissions("doulai:operLog:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(OperLogRequestBean req)
	{
		Map<String,Object> param = new HashMap<>();
		Integer type = req.getType()==null?1:req.getType();
 		if (req != null
			&&req.getInfoId() != null ){
 			//通过id查询
			SysLogininfor sysLogininfor1 = logininforService.selectById(req.getInfoId());
			//查询相临两次的登陆时间
			SysLogininfor sysLogininfor = logininforService.qryLastTimeLogin(req.getInfoId(), sysLogininfor1.getLoginName());
			if (sysLogininfor != null){
				param.put("beginTime", DateUtil.format(sysLogininfor.getLoginTime(),"yyyy-MM-dd HH:mm:ss"));
			}
			param.put("endTime",DateUtil.format(sysLogininfor1.getLoginTime(),"yyyy-MM-dd HH:mm:ss"));
			param.put("loginName",sysLogininfor1.getLoginName());
		}else {
			param.put("beginTime",DateUtil.format(req.getBeginTime(),"yyyy-MM-dd HH:mm:ss"));
			param.put("endTime",DateUtil.format(req.getEndTime(),"yyyy-MM-dd HH:mm:ss"));
			param.put("loginName",req.getLoginName());
		}

		param.put("type",req.getType());

		startPage();
        List<Map<String,Object>> list = operLogService.qryBiOperLog(param,type);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出用户列表
	 */
	@RequiresPermissions("doulai:operLog:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OperLog operLog)
    {
    	List<OperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<OperLog> util = new ExcelUtil<OperLog>(OperLog.class);
        return util.exportExcel(list, "operLog");
    }
	
	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户
	 */
	@RequiresPermissions("doulai:operLog:add")
	@Log(title = "用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(OperLog operLog)
	{		
		return toAjax(operLogService.insertOperLog(operLog));
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		OperLog operLog = operLogService.selectOperLogById(id);
		mmap.put("operLog", operLog);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户
	 */
	@RequiresPermissions("doulai:operLog:edit")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(OperLog operLog)
	{		
		return toAjax(operLogService.updateOperLog(operLog));
	}
	
	/**
	 * 删除用户
	 */
	@RequiresPermissions("doulai:operLog:remove")
	@Log(title = "用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(operLogService.deleteOperLogByIds(ids));
	}




}
